package com.zbkj.crmeb.api.controller;


import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crmeb.core.common.CommonApiResult;
import com.crmeb.core.utils.RedisUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.utils.ProductUtil;
import com.zbkj.crmeb.chant.model.ChannelMerchant;
import com.zbkj.crmeb.chant.service.IChannelMerchantService;
import com.zbkj.crmeb.store.response.StoreProductApiResponse;
import com.zbkj.crmeb.store.service.StoreProductService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 对外提供商品获取
 * @Description: 
 * @author daigb
 * @date 2021-02-24
 */
@Slf4j
@RestController
@RequestMapping("api/suyun")
@Api(tags = "对外商品列表查询")
public class ApiProductController {
    @Autowired
    private StoreProductService storeProductService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IChannelMerchantService channelMerchantService;

    /**
     * 商品列表
     * @Description:  
     * @author daigb 
     * @param @param id
     * @param @return
     * @return CommonResult<CommonPage<ProductResponse>>
     * @throws
     */
    @ApiOperation(value = "商品列表")
    @GetMapping("/product/page")
    public CommonApiResult<List<StoreProductApiResponse>> getList(HttpServletRequest request, @RequestParam(required = false) List<String> cateIds, 
            @RequestParam(required = false) List<String> filterCateIds, @RequestParam(required = false) List<String> brandIds,
            @RequestParam(required = false) String goodsName, @RequestParam(required = false) String keyWord,
            @RequestParam(required = false) Integer goodsId, @RequestParam(required = false) Integer limitStock,
            @RequestParam(required = false) Integer isShow,
            @RequestParam(defaultValue = "1") Integer pageNow, @RequestParam(defaultValue = "50") Integer pageSize) {
        // 验签
        String token = request.getHeader("token");
        String millis = request.getHeader("millis");
        String merNo = request.getHeader("merNo");
        String merSecret = request.getHeader("merSecret");
        log.info("--------------- 收到远程调用商品列表请求: {} ---------------", merNo);
        if(!token.equals(ProductUtil.getSign(millis, merNo, merSecret))) {
            return CommonApiResult.forbidden();
        }
        // 验证商户是否存在
        ChannelMerchant channelMerchant = channelMerchantService.getOne(Wrappers.<ChannelMerchant>lambdaQuery()
                .eq(ChannelMerchant :: getMerchantNo, merNo)
                .eq(ChannelMerchant :: getMerchantSecret, merSecret)
                .last("LIMIT 1")
                );
        if(null == channelMerchant) {
            return CommonApiResult.forbidden();
        }
        PageInfo<StoreProductApiResponse> result = storeProductService.listProductByApi(channelMerchant, cateIds, filterCateIds, brandIds, goodsName, keyWord, goodsId, limitStock, isShow, pageNow, pageSize);
        
        return CommonApiResult.success(result.getTotal(), result.getList());
    }
    
    /**
     * 商品查询
     * @Description:  
     * @author daigb 
     * @param @param id
     * @param @return
     * @return CommonResult<CommonPage<ProductResponse>>
     * @throws
     */
    @ApiOperation(value = "商品查询")
    @GetMapping("/product/{barCode}")
    public CommonApiResult<StoreProductApiResponse> getList(HttpServletRequest request, @ApiParam(value = "商品barcode", required = true) @PathVariable("barCode") String barCode) {
        // 验签
        String token = request.getHeader("token");
        String millis = request.getHeader("millis");
        String merNo = request.getHeader("merNo");
        String merSecret = request.getHeader("merSecret");
        log.info("--------------- 收到远程调用商品查询请求: {} ---------------", merNo);
        if(!token.equals(ProductUtil.getSign(millis, merNo, merSecret))) {
            return CommonApiResult.forbidden();
        }
        // 验证商户是否存在
        ChannelMerchant channelMerchant = channelMerchantService.getOne(Wrappers.<ChannelMerchant>lambdaQuery()
                .eq(ChannelMerchant :: getMerchantNo, merNo)
                .eq(ChannelMerchant :: getMerchantSecret, merSecret)
                .last("LIMIT 1")
                );
        if(null == channelMerchant) {
            return CommonApiResult.forbidden();
        }
        StoreProductApiResponse result = storeProductService.getProductByApi(channelMerchant, barCode);
        
        return CommonApiResult.success(null == result ? 0l : 1l, result);
    }
    
    /**
     * 有赞接口, 不维护
     * @Description:  
     * @author daigb 
     * @param @param request
     * @param @param cateIds
     * @param @param filterCateIds
     * @param @param pageNow
     * @param @param pageSize
     * @param @return
     * @return CommonApiResult<List<StoreProductApiResponse>>
     * @throws
     */
    @ApiOperation(value = "商品列表")
    @GetMapping("/products")
    public CommonApiResult<List<StoreProductApiResponse>> getList(HttpServletRequest request, @RequestParam(required = false) List<String> cateIds, @RequestParam(required = false) List<String> filterCateIds, @RequestParam(defaultValue = "1") Integer pageNow, @RequestParam(defaultValue = "50") Integer pageSize) {
        String key = request.getHeader("merchantNo");
        log.info("--------------- 收到远程调用商品列表请求: {} ---------------", key);
        if(StringUtil.isEmpty(key)) {
            return CommonApiResult.forbidden();
        }
        // 商户号判断
        ChannelMerchant channelMerchant = null;
        Object redisObj = redisUtil.get("api:key:" + key);
        if(null == redisObj) {
            channelMerchant = channelMerchantService.getOne(Wrappers.<ChannelMerchant>lambdaQuery()
                    .eq(ChannelMerchant :: getMerchantNo, key)
                    .last("LIMIT 1")
                    );
            if(null == channelMerchant) {
                log.info("--------------- 收到远程调用商品列表请求, 未查询到对应商户: {} ---------------", key);
                //redisUtil.set(key, channelMerchant, 1L, TimeUnit.MINUTES);
                return CommonApiResult.forbidden();
            } else {
                redisUtil.set("api:key:" + key, channelMerchant, 10L, TimeUnit.MINUTES);
            }
        } else {
            channelMerchant = (ChannelMerchant) redisObj;
        }
        PageInfo<StoreProductApiResponse> result = storeProductService.listProductByApi(channelMerchant, cateIds, filterCateIds, null, null, null, null, null, null, pageNow, pageSize);
        
        return CommonApiResult.success(result.getTotal(), result.getList());
    }

}



